package com.mc.lp.security.user;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author: deepak.verma
 * @description: main class to authenticate the user.
 * */
@Service("userAuthenticationService")
public class UserAuthenticationService implements UserDetailsService {
	// TODO plug in userDao here.it shall have ability fetch the user detail
	// from db based on username only.
	public UserDetails loadUserByUsername(String login) throws UsernameNotFoundException {
		boolean enabled = true;
		boolean accountNonExpired = true;
		boolean credentialsNonExpired = true;
		boolean accountNonLocked = true;
		return new User(login, "test", enabled, accountNonExpired, credentialsNonExpired, accountNonLocked,
				getAuthorities(login.equalsIgnoreCase("user") ? 1 : login.equalsIgnoreCase("admin") ? 2 : 0));
	}

	public Collection<? extends GrantedAuthority> getAuthorities(Integer role) {
		List<GrantedAuthority> authList = getGrantedAuthorities(getRoles(role));
		return authList;
	}

	public List<String> getRoles(Integer role) {
		List<String> roles = new ArrayList<String>();
		if (role.intValue() == 1) {
			roles.add("ROLE_MODERATOR");
			roles.add("ROLE_USER");
		} else if (role.intValue() == 2) {
			roles.add("ROLE_MODERATOR");
			roles.add("ROLE_ADMIN");
		}
		return roles;
	}

	public static List<GrantedAuthority> getGrantedAuthorities(List<String> roles) {
		List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();

		for (String role : roles) {
			authorities.add(new SimpleGrantedAuthority(role));
		}
		return authorities;
	}

}